package com.zhangliuliu.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhangliuliu.bean.ExwareInfoBean;
import com.zhangliuliu.bean.UserBean;
import com.zhangliuliu.service.IUserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("users")
public class UserController {
    @Resource
    private IUserService iUserService;


    @RequestMapping("login")
    @ResponseBody
    public UserBean login(String userName, String userPwd, HttpServletRequest request) {
        UserBean userBean = iUserService.login(userName, userPwd);
        HttpSession session = request.getSession();
        //如果用户存在，则将用户信息放在session中
        if (userBean != null) {
            session.setAttribute("loginUser", userBean);
        }
        //如果用户是库存管理员，则将其添加出库详情的物品集合
        if (userBean != null && userBean.getUserPower().equals("库房管理员")) {
            Map<Integer, ExwareInfoBean> exwareList = new HashMap<>();
            session.setAttribute("exwareList", exwareList);
        }
        return userBean;
    }
    @RequestMapping("get")
    @ResponseBody
    public UserBean getLoginUser(HttpServletRequest request) {
        return (UserBean) request.getSession().getAttribute("loginUser");
    }
    @RequestMapping("add")
    @ResponseBody
    void  addUser(UserBean userBean){
        iUserService.addUser(userBean);
    }
    @RequestMapping("del")
    @ResponseBody
    void delUser(int userId){
        iUserService.delUser(userId);
    }
    @RequestMapping("update")
    @ResponseBody
    void updateUser(@RequestParam("userId") int userId, @RequestParam("userPwd") String userPwd,
                    @RequestParam("power") String power){
        iUserService.updateUser(userId,userPwd,power);
    }
    @RequestMapping("cutByPower")
    @ResponseBody
    IPage<UserBean> cutByPower(@RequestParam(value = "pageNo",defaultValue = "1") int pageNo,
                               @RequestParam(value = "pageSize",defaultValue = "4") int pageSize,
                               @RequestParam("power") String power){
        return iUserService.cutByPower(pageNo,pageSize,power);
    }
    IPage<UserBean>cut(@RequestParam(value = "pageNo",defaultValue = "1") int pageNo,
                       @RequestParam(value = "pageSize",defaultValue = "4") int pageSize){
        return iUserService.cut(pageNo,pageSize);
    }
}
